package ring_buffer

import "context"

type CoverRingBuffer struct {
	*RingBuffer
}

func (c *CoverRingBuffer) Write(ctx context.Context, v interface{}) error {
	c.mu.Lock()
	defer c.mu.Unlock()
	c.queue[c.writeIdx%c.size] = v // 覆盖
	if c.isFull() {                // 满了
		c.readIdx++ // 原先的readIdx上的值被覆盖，需要往前移动
	}
	c.writeIdx++
	return nil
}

func NewCoverRingBuffer(size int) Ring {
	c := &CoverRingBuffer{
		RingBuffer: newRingBufferV2(size),
	}

	return c
}

var _ Ring = (*CoverRingBuffer)(nil)
var _ RingBufferCreator = NewCoverRingBuffer
